package com.boolib.shopdfs.web;

import com.boolib.commons.entity.ServerResponse;

import com.boolib.shopdfs.service.IDfsService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/dfs/img")
public class DfsImageController {


    @Autowired
    private IDfsService iDfsService;




//    @ResponseBody
//    @RequestMapping(value = "/file/upload", method = RequestMethod.POST)
//    public ServerResponse uploadFast(@RequestParam("file") MultipartFile file) throws Exception {
//
//
//
//        if(file.isEmpty() ){
//            return ServerResponse.createByErrorMessage("ERROR：文件上传不能空");
//        }
//
//
//
//        FastDFSClient fastDFSClient = new FastDFSClient(Const.DFS_CONF);
//
//        String ext = FilenameUtils.getExtension(file.getOriginalFilename());
//
//
//        String filePath = fastDFSClient.uploadFile(  file.getBytes(),ext);
//
//
//
//        return ServerResponse.createBySuccess("http://www.2cryq.top/"+ filePath);
//
//    }
//


    @ResponseBody
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public ServerResponse uploadFast2(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {
        return iDfsService.uploadImage(file);
    }


    @ResponseBody
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public ServerResponse save(String html){

        System.out.println(html);

        return ServerResponse.createBySuccess(html);
    }


    @ResponseBody
    @GetMapping("delete")
    public  ServerResponse delete(String uri) throws Exception {
      return iDfsService.delete(uri);
    }



    @GetMapping("download")
    public ResponseEntity<byte[]> download(String uri) throws Exception {
        return iDfsService.download(uri);
    }


}
